﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleWebsite.Models;

namespace SaleWebsite.Controllers
{
    public class DonViTinhController : Controller
    {
        //
        // GET: /DonViTinh/

        public ActionResult Index()
        {
            return View(Donvitinh.ConvertAll(Donvitinh.GetAll()));
        }

        public ActionResult insert(string name)
        {

            Donvitinh item = new Donvitinh() { Name = name};
            ulong id = item.Insert();
            item.Id = id;
            return PartialView("donvitinh_row", item);


        }
        public JsonResult delete(ulong id)
        {
            Donvitinh item = new Donvitinh() { Id = id };
            if (item.Delete() > 0)
            {
                return Json(new { status = "success" });
            }
            else
            {
                return Json(new { status = "not" });
            }

        }
        public ActionResult Save_donvitinh(string name, ulong id)
        {
            try
            {
                Donvitinh item = new Donvitinh() { Id = id, Name = name};
                if (item.Save() > 0)
                {
                    //return Json(new { error = "update Faile" });
                    return PartialView("donvitinh_row", item);
                }
                else
                {
                    return Json(new { error = "update Faile" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { error = ex.Message });
            }
        }

    }
}
